
package com.probiz.estoresa.customer.web.action;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;

import com.probiz.estore.common.model.product.ProductReview;
import com.probiz.estore.common.model.system.AppUser;
import com.probiz.estore.core.controller.GenericStoreAdminController;
import com.probiz.estore.core.event.EventUtil;
import com.probiz.estore.core.event.StoreFrontCache4ProductClearEvent;
import com.probiz.estore.core.search.SearchCriteria;
import com.probiz.estore.product.model.ProductReviewSearchCriteria;
import com.probiz.estore.product.service.ProductReviewManager;
import com.probiz.estore.util.ipwry.IPSeeker;
import com.probiz.estore.webapp.util.RequestContext;
import com.probiz.estore.webapp.util.RequestUtil;

public class ProductReviewController extends GenericStoreAdminController<ProductReview> {
	private ProductReviewManager	productReviewManager	= null;


	public void setProductReviewManager(ProductReviewManager inMgr) {
		this.productReviewManager = inMgr;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.controller.GenericController#getEntityName(java.lang.Object)
	 */
	@Override
	protected String getEntityName(ProductReview entity) {
		return entity.getSubject();
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.controller.BaseController#initController()
	 */
	@Override
	protected void initController() throws Exception {
		mgr = productReviewManager;
	}

	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.core.controller.GenericStoreAdminController#preSave(javax.servlet.http.HttpServletRequest, java.lang.Object, org.springframework.validation.BindException)
	 */
	@Override
	protected void preSave(HttpServletRequest request, ProductReview entity,
			BindException errors) {
	}
	
	public ModelAndView defaultAction(HttpServletRequest request,HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("customer/productReviewList");
		return mv;
	}
	
	public ModelAndView search(HttpServletRequest request,	HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("customer/include/productReviewListBody");
		SearchCriteria searchCriteria = createSearchCriteria(request);
		ProductReviewSearchCriteria productReviewSearchCriteria = new ProductReviewSearchCriteria();
		try {
			// 绑定参数
			bind(request, productReviewSearchCriteria);
			List<ProductReview> productReviewList = productReviewManager.searchByCriteriaForController(productReviewSearchCriteria,searchCriteria);
			mv.addObject("productReviewList", productReviewList);
			mv.addObject("productReviewSearchCriteria", productReviewSearchCriteria);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return mv;
	}
	/**
	 * 切换已阅未阅
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView multiToggleReviewReadStatus(HttpServletRequest request,	HttpServletResponse response) {
		String[] ids = request.getParameterValues("multiIds");
		if (ids != null && ids.length > 0) {
			for (String entityId : ids) {
				ProductReview productReview = productReviewManager.getById(Integer.valueOf(entityId));
				if(!productReview.getIsRead().equals(ProductReview.STATUS_READ)){
					productReviewManager.doMarkRead(productReview);
				}else{
					productReviewManager.doMarkUnRead(productReview);
				}
				
				//清空该商品页面缓存
				EventUtil.getInstance().fire(new StoreFrontCache4ProductClearEvent(this,productReview.getProductId()));
			}
		}
		return null;
	}
	/**
	 * 切换可见隐藏
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView multiToggleReviewShowStatus(HttpServletRequest request,	HttpServletResponse response) {
		String[] ids = request.getParameterValues("multiIds");
		if (ids != null && ids.length > 0) {
			for (String entityId : ids) {
				ProductReview productReview = productReviewManager.getById(Integer.valueOf(entityId));
				if(!productReview.getStatus().equals(ProductReview.STATUS_SHOW)){
					productReviewManager.doMarkShow(productReview);
				}else{
					productReviewManager.doMarkUnShow(productReview);
				}
				
				//清空该商品页面缓存
				EventUtil.getInstance().fire(new StoreFrontCache4ProductClearEvent(this,productReview.getProductId()));
			}
		}
		return null;
	}
	
	
	/**
	 * 删除
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView multiDelete(HttpServletRequest request,	HttpServletResponse response) {
		String[] ids = request.getParameterValues("multiIds");
		if (ids != null && ids.length > 0) {
			for (String entityId : ids) {
				ProductReview productReview = productReviewManager.getById(Integer.valueOf(entityId));
				productReviewManager.deleteFully(productReview);
			}
		}
		return null;
	}
	/**
	 * 切换置顶非置顶
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView multiToggleReviewTopStatus(HttpServletRequest request,	HttpServletResponse response) {
		String[] ids = request.getParameterValues("multiIds");
		if (ids != null && ids.length > 0) {
			for (String entityId : ids) {
				ProductReview productReview = productReviewManager.getById(Integer.valueOf(entityId));
				if(!productReview.getSortOrder().equals(ProductReview.STATUS_TOP)){
					productReviewManager.doMarkTop(productReview);
				}else{
					productReviewManager.doMarkUnTop(productReview);
				}
				
				//清空该商品页面缓存
				EventUtil.getInstance().fire(new StoreFrontCache4ProductClearEvent(this,productReview.getProductId()));
			}
		}
		return null;
	}
	/**
	 * 置为已读
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView markRead(HttpServletRequest request,	HttpServletResponse response){
		Map<String, Object> data = new HashMap<String, Object>();
		ProductReview productReview = productReviewManager.getById(RequestUtil.getInteger(request, "productReviewId"));
		if(!productReview.getIsRead().equals(ProductReview.STATUS_READ)){
			productReviewManager.doMarkRead(productReview);
		}
		data.put("productReviewId", productReview.getProductReviewId());
		return this.getModelAndAjaxJsonView(data);
	}
	/**
	 * 管理员回复
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView replyReview(HttpServletRequest request,HttpServletResponse response) {
		Map<String, Object> data = new HashMap<String, Object>();
		ProductReview review = productReviewManager.getById(RequestUtil.getInteger(request, "productReviewId"));
		ProductReview reply = new ProductReview();
		reply.setParent(review);
		reply.setCustomer((AppUser) RequestContext.getCurrentUser());
		reply.setRemoteIp(request.getRemoteAddr());
		reply.setAddress(IPSeeker.getInstance().getAddress(reply.getRemoteIp()));
		reply.setProduct(review.getProduct());
		reply.setMessage(RequestUtil.getParameterNullSafe(request, "replyMessage"));
		reply.setIsRead(ProductReview.STATUS_READ);
		reply.setSortOrder(ProductReview.STATUS_UNTOP);
		reply.setStatus(ProductReview.STATUS_SHOW);
		reply.setScore(new BigDecimal("0"));
		productReviewManager.addReply(reply);
		data.put("productReviewId", review.getProductReviewId());//返回评论的id
		
		//清空该商品页面缓存
		EventUtil.getInstance().fire(new StoreFrontCache4ProductClearEvent(this,review.getProductId()));
		return this.getModelAndAjaxJsonView(data);
	}
	/**
	 * 获得评论
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView getProductReview(HttpServletRequest request,HttpServletResponse response) {
		Map<String, Object> data = new HashMap<String, Object>();
		ProductReview review = productReviewManager.getById(RequestUtil.getInteger(request, "productReviewId"));
		data.put("productReviewId", review.getProductReviewId());
		data.put("replyCount", review.getReplyCount());
		data.put("onShowReplyCount", review.getOnShowReplyCount());
		return this.getModelAndAjaxJsonView(data);
	}
}
